package com.bwhx.eruler.controller;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.bwhx.eruler.bean.ChangeVersion;
import com.bwhx.eruler.bean.OfflineOrder;
import com.bwhx.eruler.bean.Order;
import com.bwhx.eruler.bean.OrderBack;
import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.bean.Ruler;
import com.bwhx.eruler.bean.Staff;
import com.bwhx.eruler.service.BuyerService;
import com.bwhx.eruler.service.ChangeVersionService;
import com.bwhx.eruler.service.OrderService;
import com.bwhx.eruler.service.RulerService;
import com.bwhx.eruler.service.StaffService;
import com.bwhx.eruler.util.DateUtils;
/**
 * 微信订单管理
 * @author Cuilitang
 *
 */

@Controller
public class ChangeVersionController extends BaseController {
	
	@Autowired
	ChangeVersionService cvService;
	
	@Autowired
	RulerService rulerService;

	

	@ResponseBody
  	@RequestMapping(value = "/page.changeVersionList.ashx")
	 public Map orderList(Integer page,Integer rows,ChangeVersion cv) {
		//每页的开始记录
        int start = (page-1)*rows;
        Page p = new Page(start,rows,cv);
		Map map = new HashMap();
		try{
			List<ChangeVersion> list = cvService.list(p);
			Integer count = cvService.count(cv);

			log.info(list);
			log.info(count);
			
			map.put("total",count );//total键 存放总记录数，必须的  
			map.put("rows",list);//rows键 存放每页记录 list 
			
		
			
		}catch(Exception e){
			log.error(getTrace(e));
			map.put("meg", "失败！");
			map.put("status", "0");
		}
		
	    return map;  
	  }
	
	

	
	@ResponseBody
	@RequestMapping(value = "/page.saveChangeVersion.ashx")
	 public Map<String,String> saveChangeVersion(@RequestBody String b,HttpSession session) throws UnsupportedEncodingException{
		
		log.info(b);
		Map<String,String> resultMap = new HashMap<>();
		resultMap.put("status", "1");
		ChangeVersion cv = new ChangeVersion();
		List<Ruler> paramRulers = new ArrayList<Ruler>();
		List<Ruler> rusultRulers = new ArrayList<Ruler>();
		Map<String, String> map = new HashMap<String, String>();
		HashMap<String, Integer> paramMap = new HashMap<>();

		String[] split = b.split("&");
		for (String string : split) {
			String[] keyValue = string.split("=");
			map.put(keyValue[0], keyValue[1]);
		}
		
		
		//code和版本列表
		Integer oPNumber = Integer.parseInt(map.get("opnumber"));
		StringBuilder sb = new StringBuilder();
		for(int i=1;i<=oPNumber;i++){
			String opc=map.get("opcodeinput_"+i);
			String opversion=map.get("copversion_"+i);
			sb.append(opc+',');
			Ruler ruler = new Ruler();
			ruler.setRsn(opc);
			ruler.setrVersion(Integer.parseInt(opversion));
			paramRulers.add(ruler);
			
		}
		
		for (Ruler ruler : paramRulers) {
			Ruler resultRuler = rulerService.findByRSN(ruler.getRsn());
			
			if(null==resultRuler.getrBuyerId()){
				resultMap.put("status", "nobuyer");
				resultMap.put("RSN", ruler.getRsn());
				return resultMap;
			}
			else if(resultRuler.getrBuyerId()!=Integer.parseInt(map.get("cVBuyerId"))){
				resultMap.put("status", "notbelong");
				resultMap.put("RSN", ruler.getRsn());
				return resultMap;
			}
			
			rusultRulers.add(resultRuler);
		}
		
		//修改客户名下E尺个数
		if(1==Integer.parseInt(map.get("cVType"))){
			
			Integer number0=0;
			Integer number1=0;
			Integer number2=0;
			Integer number3=0;
			
			for (Ruler ruler : paramRulers) {
				switch (ruler.getrVersion()) {
					case 0:
						number0++;
						break;
					case 1:
						number1++;
						break;
					case 2:
						number2++;
						break;
					case 3:
						number3++;
						break;
				}
			}
			
			for (Ruler ruler : rusultRulers) {
				switch (ruler.getrVersion()) {
					case 0:
						number0--;
						break;
					case 1:
						number1--;
						break;
					case 2:
						number2--;
						break;
					case 3:
						number3--;
						break;
				}
			}
			
			paramMap.put("id", Integer.parseInt(map.get("cVBuyerId")));
			paramMap.put("number0", number0);
			paramMap.put("number1", number1);
			paramMap.put("number2", number2);
			paramMap.put("number3", number3);
			
		}
				
				
		
		
		cv.setcVSNS(sb.toString().substring(0, sb.length()-1));
		
		cv.setcVDoneTime(System.currentTimeMillis()/1000);
		cv.setcVIsDone(1);
		cv.setcVType(Integer.parseInt(map.get("cVType")));
		cv.setcVBuyerId(Integer.parseInt(map.get("cVBuyerId")));
		cv.setcVProposerId(Integer.parseInt(map.get("cVProposerId")));
		cv.setcVStorekeeperId(Integer.parseInt(map.get("cVStorekeeperId")));
		cv.setcVNumber(oPNumber);
		cv.setcVRemark(URLDecoder.decode(map.get("cVRemark"),"utf-8"));
		
		
		cvService.insert(cv,paramRulers,paramMap);
				
		return resultMap;  
	  }
	
	
}
